package com.xcexample.electronicclasscard

import android.app.Activity
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.annotation.RawRes
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.LazyHeaders
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.tencent.mmkv.MMKV
import java.io.File

object GlideHelper {
    fun loadWithRoundCorner(imageView: ImageView, image: Any, corner: Int) {
        Glide.with(App.instance).asBitmap().load(image)
            .apply(RequestOptions().transform(CenterCrop(), RoundedCorners(corner)))
            .into(imageView)
    }

    fun loadWithRoundCorner(imageView: ImageView, bitmap: Bitmap, corner: Int) {
        Glide.with(App.instance).asBitmap().load(bitmap)
            .apply(RequestOptions().transform(CenterCrop(), RoundedCorners(corner)).override(100))
            .into(imageView)
    }

    fun loadImage(imageView: ImageView, url: String) {
        var  glideUrl =  GlideUrl(url,  LazyHeaders.Builder()
            .addHeader("token", MMKV.defaultMMKV().decodeString("token")?:"")
            .build())
        Glide.with(App.instance).load(glideUrl).skipMemoryCache(true)
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .into(imageView)
    }

    fun loadImage(imageView: ImageView, file: File) {
        Glide.with(App.instance).load(file)
            .into(imageView)
    }

    fun loadWithRoundCorner(
        imageView: ImageView,
        @RawRes @DrawableRes res: Int,
        corner: Int
    ) {
        Glide.with(App.instance).load(res)
            .apply(RequestOptions().transform(CenterCrop(), RoundedCorners(corner)))
            .into(imageView)
    }

    fun loadWithRoundCorner(imageView: ImageView, res: Drawable, corner: Int) {
        Glide.with(App.instance).load(res)
            .apply(RequestOptions().transform(CenterCrop(), RoundedCorners(corner)))
            .into(imageView)
    }

    fun loadWithRoundCorner(imageView: ImageView, url: String, corner: Int) {
        Glide.with(App.instance).load(url)
            .apply(RequestOptions().transform(CenterCrop(), RoundedCorners(corner)))
            .into(imageView)
    }

    fun loadWithRoundCorner(
        imageView: ImageView, url: String, corner: Int,
        trans: BitmapTransformation
    ) {
        Glide.with(App.instance).load(url)
            .apply(RequestOptions().transform(trans, RoundedCorners(corner)))
            .into(imageView)
    }

    fun loadWithRoundCorner(
        imageView: ImageView, url: String,
        @RawRes @DrawableRes placeholderId: Int, corner: Int
    ) {
        Glide.with(App.instance).load(url)
            .error(placeholderId)
            .placeholder(placeholderId)
            .apply(RequestOptions().transform(CenterCrop(), RoundedCorners(corner)))
            .into(imageView)
    }

    fun loadWithRoundCornerWH(
        imageView: ImageView, url: String, corner: Int, width: Int,
        height: Int
    ) {
        Glide.with(App.instance).load(url)
            .apply(RequestOptions().transform(CenterCrop(), RoundedCorners(corner)))
            .override(width, height)
            .into(imageView)
    }

    fun loadWithRoundCorner(
        imageView: ImageView, url: String, corner: Int, width: Int,
        height: Int
    ) {
        Glide.with(App.instance).asBitmap().load(url)
            .apply(RequestOptions().transform(CenterCrop(), RoundedCorners(corner)))
            .override(width, height)
            .into(imageView)
    }

    /**
     * 圆形图片
     * @param context
     * @param imageView
     * @param source
     * @param placholdId
     * @param errId
     */
    fun loadCircleImage(
        context: Any?,
        imageView: ImageView,
        source: Any?,
        placholdId: Int,
        errId: Int = placholdId
    ) {
        val options = RequestOptions()
            .priority(Priority.HIGH)
            .placeholder(placholdId)
            .error(errId)
            .dontAnimate()
            .circleCrop()
        loadImage(context, imageView, source, options)
    }

    private fun loadImage(
        context: Any?,
        imageView: ImageView,
        source: Any?,
        options: RequestOptions
    ) {
        checkContext(context)
        if (context is Activity) {
            Glide.with((context as Activity?)!!)
                .load(source)
                .apply(options)
                .into(imageView!!)
        } else if (context is Fragment) {
            Glide.with((context as Fragment?)!!)
                .load(source)
                .apply(options)
                .into(imageView)
        }
    }

    fun loadWithRoundCorner(
        imageView: ImageView, @RawRes @DrawableRes resourceId: Int,
        @RawRes @DrawableRes placeholderId: Int, corner: Int
    ) {
        Glide.with(App.instance).asBitmap().load(resourceId)
            .error(placeholderId)
            .placeholder(placeholderId)
            .apply(RequestOptions().transform(CenterCrop(), RoundedCorners(corner)))
            .into(imageView)
    }

    fun loadGif(imageView: ImageView, @RawRes @DrawableRes resourceId: Int) {
        Glide.with(App.instance)
            .asGif()
            .load(resourceId)
            .apply(RequestOptions().fitCenter().diskCacheStrategy(DiskCacheStrategy.DATA))
            .into(imageView)
    }

    private fun checkContext(context: Any?) {
        if (context !is Activity && context !is Fragment) {
            throw RuntimeException("context 必须是 Activity 或 Fragment")
        }
    }
}